package com.icim.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.icim.entity.Ret_tm_cus_prd_indt;
import com.icim.entity.Ret_wo;

@Repository
public class RetTmCusPrdIndtDao {
    @Resource
    private JdbcTemplate icimJdbcTemplate;

    private Ret_tm_cus_prd_indt setEntity(ResultSet rs) throws SQLException {
        Ret_tm_cus_prd_indt retTmCusPrdIndt = new Ret_tm_cus_prd_indt();

        retTmCusPrdIndt.setFactoryname(rs.getString("FACTORYNAME"));
        retTmCusPrdIndt.setProductrequestname(rs.getString("PRODUCTREQUESTNAME"));
        retTmCusPrdIndt.setBoxname(rs.getString("BOXNAME"));
        retTmCusPrdIndt.setProductspecname(rs.getString("PRODUCTSPECNAME"));
        retTmCusPrdIndt.setLotname(rs.getString("LOTNAME"));
        retTmCusPrdIndt.setProductname(rs.getString("PRODUCTNAME"));
        retTmCusPrdIndt.setPosition(rs.getInt("POSITION"));
        retTmCusPrdIndt.setProductgrade(rs.getString("PRODUCTGRADE"));

        return retTmCusPrdIndt;
    }

    public void saveBatch(List<Ret_tm_cus_prd_indt> dtList) {
        StringBuffer sql = new StringBuffer("INSERT INTO RET_TM_CUS_PRD_INDT");
        sql.append("(");
        sql.append("FACTORYNAME,PRODUCTREQUESTNAME,PRODUCTSPECNAME,BOXNAME,LOTNAME,PRODUCTNAME,POSITION,PRODUCTGRADE");
        sql.append(")");
        sql.append("VALUES(?,?,?,?,?,?,?,?)");
        List<Object[]> entityList = new ArrayList<Object[]>();
        for (Ret_tm_cus_prd_indt dt : dtList) {
            Object[] objs = { dt.getFactoryname(), dt.getProductrequestname(), dt.getProductspecname(),
                    dt.getBoxname(), dt.getLotname(), dt.getProductname(), dt.getPosition(), dt.getProductgrade() };
            entityList.add(objs);
        }
        icimJdbcTemplate.batchUpdate(sql.toString(), entityList);
    }

    public List<Ret_tm_cus_prd_indt> list(String boxId) {
        String sql = "SELECT * FROM RET_TM_CUS_PRD_INDT WHERE BOXNAME = ? ORDER BY POSITION";

        return icimJdbcTemplate.query(sql, new Object[] { boxId }, new RowMapper<Ret_tm_cus_prd_indt>() {

            @Override
            public Ret_tm_cus_prd_indt mapRow(ResultSet rs, int rowNum) throws SQLException {
                return setEntity(rs);
            }

        });
    }

    public Ret_tm_cus_prd_indt get(String productname) {

        String sql = "SELECT * FROM RET_TM_CUS_PRD_INDT WHERE PRODUCTNAME = ?";

        List<Ret_tm_cus_prd_indt> retTmCusPrdIndtList = icimJdbcTemplate.query(sql, new Object[] { productname },
                new RowMapper<Ret_tm_cus_prd_indt>() {

                    @Override
                    public Ret_tm_cus_prd_indt mapRow(ResultSet rs, int rowNum) throws SQLException {
                        return setEntity(rs);
                    }

                });

        return retTmCusPrdIndtList.isEmpty() ? null : retTmCusPrdIndtList.get(0);

    }

    public void save(Ret_tm_cus_prd_indt dt) {
        StringBuffer sql = new StringBuffer("INSERT INTO RET_TM_CUS_PRD_INDT");
        sql.append("(");
        sql.append("FACTORYNAME,PRODUCTREQUESTNAME,PRODUCTSPECNAME,BOXNAME,LOTNAME,PRODUCTNAME,POSITION,PRODUCTGRADE");
        sql.append(")");
        sql.append("VALUES(?,?,?,?,?,?,?,?)");

        icimJdbcTemplate
                .update(sql.toString(),
                        new Object[] { dt.getFactoryname(), dt.getProductrequestname(), dt.getProductspecname(),
                                dt.getBoxname(), dt.getLotname(), dt.getProductname(), dt.getPosition(),
                                dt.getProductgrade() });

    }

    public void update(Ret_tm_cus_prd_indt dt) {
        StringBuffer sql = new StringBuffer("UPDATE RET_TM_CUS_PRD_INDT SET ");
        sql.append("FACTORYNAME = ? ,PRODUCTREQUESTNAME = ? ,PRODUCTSPECNAME=?,");
        sql.append("BOXNAME=?,LOTNAME=?,POSITION=?,");
        sql.append("PRODUCTGRADE=?");
        sql.append("WHERE PRODUCTNAME = ? ");

        icimJdbcTemplate
                .update(sql.toString(),
                        new Object[] { dt.getFactoryname(), dt.getProductrequestname(), dt.getProductspecname(),
                                dt.getBoxname(), dt.getLotname(), dt.getPosition(), dt.getProductgrade(),
                                dt.getProductname() });
    }

}
